package net.ojbk.study.twobox.service;

import net.ojbk.study.twobox.beans.po.Consult;
import net.ojbk.study.twobox.beans.po.Refer;
import net.ojbk.study.twobox.beans.po.Staff;
import net.ojbk.study.twobox.beans.po.Student;
import net.ojbk.study.twobox.dao.ConsultMapper;
import net.ojbk.study.twobox.dao.StaffMapper;
import net.ojbk.study.twobox.dao.StudentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.BindingResult;

/**
 * Created with IntelliJ IDEA.
 * User: twobox
 * Date: 2019/4/16
 * Time: 9:13
 * Description: No Description
 */
@Service
public class ReferService extends SupperService<Refer> {

    @Autowired
    StaffMapper staffMapper;

    @Autowired
    ConsultMapper consultMapper;

    @Autowired
    StudentMapper studentMapper;
    /**
     * 增
     *
     * @param o
     * @param br
     */
    @Override
    public void addOne(Refer o, BindingResult br) throws Exception {
        this.validated(br, Refer.class);
        Staff staff = staffMapper.selectByPrimaryKey(o.getUserid());
        if (staff == null)
            throw new Exception("此员工不存在");
        this.dao.insert(o);
    }

    /**
     * 删
     *
     * @param id
     */
    @Override
    public void deleteOne(int id) throws Exception {
        Refer refer = this.dao.selectByPrimaryKey(id);

        Consult consult = new Consult();
        consult.setReferid(id);
        Student student = new Student();
        student.setReferid(id);

        if (consultMapper.selectCount(consult) <=0 || studentMapper.selectCount(student) <= 0)
            throw new Exception("其它表有此组件");

        this.dao.deleteByPrimaryKey(id);
    }

    /**
     * 改
     *
     * @param o
     * @param br
     */
    @Override
    public void updateOne(Refer o, BindingResult br) throws Exception {
        this.validated(br, Refer.class);
        this.dao.updateByPrimaryKeySelective(o);
    }
}
